* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\DSBC"

*Merge predictions from different utility and noise specifications
*ChoicePredictionFechnerCARAKTHDP also containst the TWT predictions
use Th3NoPressureBar, clear
merge 1:1 subj  using Th3TimePressureBars, clear
merge 1:1 subj  using Th3TimePressurePies, clear
merge 1:1 subj using Th3NoPressurePies, clear

gen dist = (distNopressureBar + distTimePressureBars + distTimePressurePies + distNoPressurePies)/4
gen distCRRA = (distCRRANopressureBar + distCRRATimePressureBars + distCRRATimePressurePies + distCRRANoPressurePies)/4
gen distCARA  = (distCARANopressureBar + distCARATimePressureBars + distCARATimePressurePies + distCARANoPressurePies)/4
gen distRPMCRRA = (distRPMCRRANopressureBar + distRPMCRRATimePressureBars + distRPMCRRATimePressurePies + distRPMCRRANoPressurePies)/4
gen distRPMCARA  = (distRPMCARANopressureBar + distRPMCARATimePressureBars + distRPMCARATimePressurePies + distRPMCARANoPressurePies)/4

*Summarize and tests the absolute error distance
sum dist  distCRRA  distCARA  distRPMCRRA  distRPMCARA 
signrank dist  = distCRRA
signrank dist  = distCARA
signrank dist  = distRPMCRRA
signrank dist  = distRPMCARA 

*Produce Figure 3 Left
preserve
gen G =_n
sum  dist, detail
gen graphEPV = r(mean)*100 if G==1
ci mean dist, level(95)
gen low = r(lb)   if G==1
gen high = r(ub)  if G==1
sum distCRRA , detail
replace graphEPV = r(mean)*100 if G==3
ci mean distCRRA, level(95)
replace low = r(lb)   if G==3
replace high = r(ub)  if G==3
sum distCARA, detail
replace graphEPV = r(mean)*100 if G==5
ci mean distCARA, level(95)
replace low = r(lb)   if G==5
replace high = r(ub)  if G==5
sum distRPMCRRA, detail
replace graphEPV = r(mean)*100 if G==7
ci mean distRPMCRRA, level(95)
replace low = r(lb)   if G==7
replace high = r(ub)  if G==7
sum distRPMCARA, detail
replace graphEPV = r(mean)*100 if G==9
ci mean distRPMCARA, level(95)
replace low = r(lb)   if G==9
replace high = r(ub)  if G==9
keep if G<10
replace low= low*100
replace high =high*100
#delimit ;
twoway 
(bar  graphEPV G if G ==1 ,lcolor(black)  fcolor(black)  )
(bar  graphEPV G if G ==3 ,lcolor(black)  fcolor(gs5)  )
(bar  graphEPV G if G ==5 ,lcolor(black)  fcolor(gs8)  )
(bar  graphEPV G if G ==7 ,lcolor(black)  fcolor(gs12)  )
(bar  graphEPV G if G ==9 ,lcolor(black)  fcolor(gs15)  )
(rcap high   low G, lcolor(red))
,
  graphregion(color(white) fcolor(white) lcolor(white)  ) plotregion(fcolor(white) lcolor(white) )
 ytitle("Mean Absolute Errors (%)" " ", size(large)) 
     ylabel(0(10)60, grid) title("KTHDP")
 xlabel(1 "TWT" 3"RUM-CRRA" 5"RUM-CARA" 7"RPM-CRRA" 9"RPM-CARA", noticks   )
xtitle("  ")
legend(off)  
 name(Figure3Left, replace) saving(Figure3Left, replace)
;#delimit cr
graph export Figure3Left.eps, as(eps) preview(on) replace
restore
